package com.movitech.mobile.cz.base.supplier.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.cz.base.common.util.PageCode;
import com.movitech.mobile.cz.base.modules.config.EvaluationStatusEnum;
import com.movitech.mobile.cz.base.supplier.entity.SSuppliersRank;
import com.movitech.mobile.cz.base.supplier.entity.ext.ExtSSuppliersRank;
import com.movitech.mobile.cz.base.supplier.entity.ext.SupplierRankReqVO;
import com.movitech.mobile.cz.base.supplier.mapper.SSuppliersRankMapper;
import com.movitech.mobile.cz.base.supplier.mapper.ext.ExtSSuppliersRankMapper;
import com.movitech.mobile.cz.base.supplier.service.SSuppliersRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评分定级 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@Service("sSuppliersRankService")
@Transactional
public class SSuppliersRankServiceImpl extends ServiceImpl<SSuppliersRankMapper, SSuppliersRank> implements SSuppliersRankService {

    @Autowired
    private SSuppliersRankMapper sSuppliersRankMapper;
    @Autowired
    private ExtSSuppliersRankMapper extSSuppliersRankMapper;

    @Override
    public ExtSSuppliersRank getById(String id) {
        return extSSuppliersRankMapper.getById(id);
    }

    @Override
    public List<ExtSSuppliersRank> launchSupplierRanList(SupplierRankReqVO supplierRankReqVO) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rankYear", supplierRankReqVO.getRankYear());
        map.put("supplierCategoryId", supplierRankReqVO.getSupplierCategoryId());
        map.put("suppliersName", supplierRankReqVO.getSuppliersName());
        map.put("starLevel", supplierRankReqVO.getStarLevel());
        map.put("launch", "yes");
        map.put("evaluationStatusIn", "("+EvaluationStatusEnum.EVALUATION_LEVEL_STATUS_1.getCode()+
                ","+EvaluationStatusEnum.EVALUATION_LEVEL_STATUS_4.getCode()+")");
        map.put("isReady", 0);
        return  extSSuppliersRankMapper.supplierRanList(map);
    }

    @Override
    public PageCode supplierRanList(SupplierRankReqVO supplierRankReqVO) {
        PageCode pageCode = new PageCode(supplierRankReqVO.getPageNumber(), supplierRankReqVO.getPageSize());

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rankYear", supplierRankReqVO.getRankYear());
        map.put("supplierCategoryId", supplierRankReqVO.getSupplierCategoryId());
        map.put("suppliersName", supplierRankReqVO.getSuppliersName());
        map.put("suppliersId", supplierRankReqVO.getSuppliersId());
        map.put("starLevel", supplierRankReqVO.getStarLevel());
        map.put("evaluationStatus",supplierRankReqVO.getEvaluationStatus());
        pageCode.setCount(extSSuppliersRankMapper.supplierRanListCount(map));

        map.put("currentRecordNum", (pageCode.getPageNo() - 1) * pageCode.getPageSize());
        map.put("pageSize", pageCode.getPageSize());

        List<ExtSSuppliersRank> ls = extSSuppliersRankMapper.supplierRanList(map);
        pageCode.setList(ls);
        return pageCode;
    }

    @Override
    public boolean updateStatusByIds(Integer oldStatus, Integer newStatus, String[] ids) {
        EntityWrapper entityWrapper = new EntityWrapper();
        if(oldStatus!=null)
            entityWrapper.eq("evaluation_status",oldStatus);
        entityWrapper.in("id",ids);
        SSuppliersRank update = new SSuppliersRank();
        update.setEvaluationStatus(newStatus);
        return sSuppliersRankMapper.update(update,entityWrapper)==ids.length;
    }
}
